package top.january.codelabf2;

import ohos.ace.ability.AceInternalAbility;
import ohos.agp.window.service.WindowManager;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;
import ohos.global.resource.Resource;
import ohos.global.systemres.ResourceTable;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.MessageOption;
import ohos.rpc.MessageParcel;
import ohos.rpc.RemoteException;
import ohos.utils.zson.ZSONArray;
import ohos.utils.zson.ZSONObject;

import java.util.HashMap;
import java.util.List;


public class InMainAbility extends AceInternalAbility implements AceInternalAbility.AceInternalAbilityHandler {
    private static HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x20100, "JSApp");
    private static InMainAbility m_instance = null;
    private static final String BUNDLE_NAME = "top.january.codelabf2";
    private static final String ABILITY_NAME = InMainAbility.class.getName();
    static int ACTION_FLOW = 1001;
    static int ACTION_FULLSCREEN = 1002;
    static int ACTION_EXIT_FULLSCREEN = 1003;
    static int ACTION_SAVE_CTX = 1004;
    static int ACTION_LOAD_CTX = 1005;

    MainAbility m_ctx;
    InMainAbility(String bundleName, String abilityName) {
        super(bundleName, abilityName);
    }

    public static InMainAbility getInstance() {
        if (m_instance == null) {
            synchronized (InMainAbility.class) {
                if (m_instance == null) {
                    m_instance = new InMainAbility(BUNDLE_NAME, ABILITY_NAME);
                }
            }
        }
        return m_instance;
    }

    @Override
    public boolean onRemoteRequest(int i, MessageParcel messageParcel, MessageParcel reply, MessageOption messageOption) throws RemoteException {
        String dataStr = messageParcel.readString();
        HiLog.info(LABEL_LOG, "Recv request code %{public}d", i);
        if (i == ACTION_FULLSCREEN) {
            // 必须在UI线程操作UI！！！
            m_ctx.getUITaskDispatcher().asyncDispatch(new Runnable() {
                @Override
                public void run() {
                    m_ctx.fullScreen();
                }
            });
        } else if(i == ACTION_EXIT_FULLSCREEN) {
            m_ctx.getUITaskDispatcher().asyncDispatch(new Runnable() {
                @Override
                public void run() {
                    m_ctx.exitFullScreen();
                }
            });
        } else if(i == ACTION_SAVE_CTX) {
            MyApplication app = (MyApplication)m_ctx.getAbilityPackage();
            app.global = ZSONObject.stringToZSON(dataStr);
            HiLog.info(LABEL_LOG, "save ctx %{public}s", dataStr);
        } else if (i == ACTION_LOAD_CTX) {
            MyApplication app = (MyApplication)m_ctx.getAbilityPackage();
            if (app.global == null) {
                app.loadData();
            }
            dataStr = app.global.toString();
            HiLog.info(LABEL_LOG, "load ctx %{public}s", dataStr);
            reply.writeString(dataStr);
        }

        HiLog.info(LABEL_LOG, "request finish");
        return true;
    }


    public void bind(MainAbility ctx) {
        HiLog.debug(LABEL_LOG, "Bind internal ability");
        m_ctx = ctx;
        setInternalAbilityHandler(this);
    }
    public void unbind() {
        HiLog.debug(LABEL_LOG, "Unbind internal ability");
        m_ctx = null;
        setInternalAbilityHandler(null);
    }

    String findDevice() {
        ZSONArray r = new ZSONArray();
        List<DeviceInfo> deviceList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);
        if (deviceList.size() == 0) {
            HiLog.debug(LABEL_LOG, "no device");
        }
        for(DeviceInfo deviceInfo : deviceList) {
            HiLog.debug(LABEL_LOG, "device name: %{public}s\n" +
                    "device type: %{public}s",
                    deviceInfo.getDeviceName(),
                    deviceInfo.getDeviceType().name());
            HashMap<String, Object> map = new HashMap<>();
            map.put("name", deviceInfo.getDeviceName());
            map.put("type", deviceInfo.getDeviceType().name());
            r.add(new ZSONObject(map));
        }
        return r.toString();
    }


}
